//引入四个常用通用样式
import "../../assets/css/index.less";
//引入当前样式表
import "../reg/reg.less";
//引入动画
require("animate.css");
//引入提示框
import { toats } from "../../util/toats.js";
//引入分装好的axios
import { postLoginReq } from "../../api/user.js";
//引入节点封装函数
let dom = require("../../util/dom.js");
//
dom.$("#PassWordBtn").addEventListener("click", () => {
	location.href = "./login.html";
});
//判断
dom.$("#btn").addEventListener("click", async () => {
	if (
		//手机号判断
		!/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(
			dom.$("#phone").value
		)
	) {
		return (dom.$("#feedback").innerHTML = "手机号不正确");
	}
	if (!/^(?=.*[a-zA-Z]).{1,9}/.test(dom.$("#password").value)) {
		//密码正则判断
		return (dom.$("#feedback").innerHTML = "至少包含1种至少包含字母,1-9位");
	} else {
		//验证全部通过，开始清空提示并且发送ajax
		dom.$("#feedback").innerHTML = "";
		let res = await postLoginReq({
			account: dom.$("#phone").value,
			password: dom.$("#password").value,
		});
		console.log(res.data.data.token);
		//结构赋值
		let { errno, message } = res.data;

		if (errno === 0) {
			toats("登陆成功");
			sessionStorage.setItem("token", res.data.data.token);
			setTimeout(() => {
				location.href = "./home.html";
			}, 2000);
		} else {
			console.log(message);
			toats(message);
		}
	}
});
